package com.opengroupe.dto;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.opengroupe.database.qRHSQLiteHelper;


public class QuestionDto implements Serializable {
	private static final long serialVersionUID = 74439286227161718L;
	private Integer idQuestion;
	private Integer idCategorie;
	private CategorieDto categorie;
	private String contenuQuestion;
	private List<ReponseDto> reponses;

	public QuestionDto() {
	}
	
	public QuestionDto(int idQuestion,String contenuQuestion, int idCategorie) {
		this.idQuestion = idQuestion;
		this.contenuQuestion = contenuQuestion;
		this.idCategorie = idCategorie;
	}

	

	public Integer getIdQuestion() {
		return this.idQuestion;
	}

	public void setIdQuestion(Integer idQuestion) {
		this.idQuestion = idQuestion;
	}
	public int getIdCategorie() {
		return this.idCategorie;
	}

	public void setIdCategorie(int idCategorie) {
		this.idCategorie = idCategorie;
	}
	
	public CategorieDto getCategorie() {
		return this.categorie;
	}

	public void setCategorie(CategorieDto categorie) {
		this.categorie = categorie;
	}

	public String getContenuQuestion() {
		return this.contenuQuestion;
	}

	public void setContenuQuestion(String contenuQuestion) {
		this.contenuQuestion = contenuQuestion;
	}


	public List<ReponseDto> getReponses() {
		return this.reponses != null ? this.reponses : new ArrayList<ReponseDto>();
	}

	public void setReponses(List<ReponseDto> reponses) {
		this.reponses = reponses;
	}

	
	public void save(Context context)
	{
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getWritableDatabase();
		
		
		
		ContentValues values = new ContentValues();
		values.put("contenu", contenuQuestion);
		
		if (idQuestion == 0)
		{
			values.put("idCategorie", idCategorie);
			
			// insert
			long id= bdd.insert("question", null, values);
			idQuestion = (int) id;
		}
		else
		{
			bdd.update("question", values, "id=?",new String[] { String.valueOf( this.idQuestion) });
			
		}
		
		
		bdd.close();
		bddHelper.close();
		
	}
	
	public void delete(Context context)
	{
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getWritableDatabase();			
		
		bdd.delete("question", "id=?",new String[] { String.valueOf( this.idQuestion) });
		
		bdd.close();
		bddHelper.close();
		
	}

	public static QuestionDto getFromBdd(Context context, int idQuestion) {
		QuestionDto question = null;
		
		qRHSQLiteHelper bddHelper = new qRHSQLiteHelper(context);
		SQLiteDatabase bdd = bddHelper.getReadableDatabase();
	    
	    Cursor cursor = null;
	    
	    //Question
	    cursor = bdd.query("question",
	    		new String[] { "id","contenu", "idCategorie" },
	    		"id=?", 
	    		new String[] {String.valueOf(idQuestion) },
	    		null,null,null,null);
	    
		

		cursor.moveToFirst();
		
		
	    if  (!cursor.isAfterLast()) {
	    	question = new QuestionDto(
	    			cursor.getInt(0),
	    			cursor.getString(1),
	    			cursor.getInt(2));
	    	
	    }
	    cursor.close();
	    
	    // Categorie
	    cursor = bdd.query("categorie",
	    		new String[] { "id","libelle", "idQuestionnaire" },
	    		"id=?", 
	    		new String[] {String.valueOf( question.getIdCategorie() ) },
	    		null,null,null,null);
	    
		

		cursor.moveToFirst();
		
		
	    if  (!cursor.isAfterLast()) {
	    	question.setCategorie( new CategorieDto(
	    			cursor.getInt(0),
	    			cursor.getString(1),
	    			cursor.getInt(2)));
	    	
	    }
	    cursor.close();
	    
	    
	    
	    // Questionnaire
	    cursor = bdd.query("questionnaire",
	    		new String[] { "id","libelle" },
	    		"id=?", 
	    		new String[] {String.valueOf( question.getCategorie().getIdQuestionnaire()) },
	    		null,null,null,null);
	    
	    cursor.moveToFirst();
	    
	    if  (!cursor.isAfterLast()) {
	    	question.getCategorie().setQuestionnaire( new QuestionnaireDto(
	    			cursor.getInt(0),
	    			cursor.getString(1)));
	    	
	    }
	    cursor.close();
    
	    // Reponses
   	    
	    cursor = bdd.query("reponse",
				new String[]  { "id",	"contenu", "idQuestion", "correcte" },
				"idQuestion=?", 
				new String[] { String.valueOf( idQuestion) },
				null,null,null,null);
		
		cursor.moveToFirst();
		
		ReponseDto reponse = null;
		ArrayList<ReponseDto> reponses = new ArrayList<ReponseDto>();
		
		while  (!cursor.isAfterLast()) {
			
						
			reponse = new ReponseDto(cursor.getInt(0), cursor.getString(1), cursor.getInt(3) != 0,  cursor.getInt(2));
			
			
			reponses.add(reponse);
			
			cursor.moveToNext();
		}
		
		cursor.close();
		
		question.setReponses(reponses);
		
	    bdd.close();
	    bddHelper.close();
	    
		return question;
	}
	
	@Override
	public boolean equals(Object o){
		if (o==null) return false;
		
		QuestionDto question = (QuestionDto) o;
		if (question == null) return false;
		
		return question.getIdQuestion() == this.idQuestion;
	}
}
